package com.jy.adscm.operation.cgxsb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.base.bean.JyCustomerProvider;
import com.jy.adscm.operation.base.bean.JyGoods;
import com.jy.adscm.operation.base.mapper.mysql.JyCustomerProviderMapper;
import com.jy.adscm.operation.base.mapper.mysql.JyGoodsMapper;
import com.jy.adscm.operation.cgxsb.bean.*;
import com.jy.adscm.operation.cgxsb.mapper.mysql.CgxsTaskinfoMapper;
import com.jy.adscm.operation.cgxsb.service.CgxsTaskinfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.operation.hscrb.bean.CpfhGoodsinfo;
import com.jy.adscm.operation.purchase.bean.Supplier;
import com.jy.adscm.operation.purchase.mapper.mysql.SupplierMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-10
 */
@Service
public class CgxsTaskinfoServiceImpl extends ServiceImpl<CgxsTaskinfoMapper, CgxsTaskinfo> implements CgxsTaskinfoService {
    @Autowired
    CgxsTaskinfoMapper cgxsTaskinfoMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    JyGoodsMapper jyGoodsMapper;

    @Override
    public List<CgxsTaskinfo> queryByRwlx(String rwlx, String rwzt, String rwwczt,String key) {

        List<CgxsTaskinfo> cgxsTaskinfos = cgxsTaskinfoMapper.queryByRwlx(rwlx, rwzt, rwwczt,key);
        for (CgxsTaskinfo cgxsTaskinfo : cgxsTaskinfos) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxsTaskinfo.getKfry(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgxsTaskinfo.getKfry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    cgxsTaskinfo.setKfryName(jyUser.getName());
                }
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxsTaskinfo.getCwry(),null)){
                JyUser jyUser = jyUserMapper.selectById(cgxsTaskinfo.getCwry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null));
                cgxsTaskinfo.setCwryName(jyUser.getName());
            }
        }
        return cgxsTaskinfos;
    }

    @Override
    public List<khxx> queryByKhxx(String type) {
        return cgxsTaskinfoMapper.queryByKhxx(type);
    }

    @Override
    public List<CgUser> queryByYY(String roleid) {

        return cgxsTaskinfoMapper.queryByYY(roleid);
    }

    @Override
    public List<CgxsTaskinfo> selectById(String tid) {

        CgxsTaskinfo cgxsTaskinfo = super.selectById(tid);
        List<CgxsTaskinfo> list =new ArrayList<>();

        if (null!=cgxsTaskinfo.getFzry()&&!"".equals(cgxsTaskinfo.getFzry())){
            JyUser jyUser = jyUserMapper.selectById(cgxsTaskinfo.getFzry());
            cgxsTaskinfo.setFzryName(jyUser.getName());
        }
        if (null!=cgxsTaskinfo.getCwry()&&!"".equals(cgxsTaskinfo.getCwry())){
            JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getCwry());
            cgxsTaskinfo.setCwry(jyUser.getName());
        }
        if (null!=cgxsTaskinfo.getKfry()&&!"".equals(cgxsTaskinfo.getKfry())){
            JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getKfry());
            cgxsTaskinfo.setKfry(jyUser.getName());
        }
        if(null!=cgxsTaskinfo.getXsry()&&!"".equals((cgxsTaskinfo.getXsry()))){
            JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getXsry());
            cgxsTaskinfo.setXsry(jyUser.getName());
        }
        list.add(cgxsTaskinfo);
        return list;
    }

    @Override
    public Page<CgxsTaskinfo> selectCgxsTaskinfo(Page<CgxsTaskinfo> page, QueryCgxsTaskinfo queryCgxsTaskinfo) {
                Wrapper<CgxsTaskinfo> wrapper=new EntityWrapper<>();
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getRwlx(),null)){
                    wrapper.eq("RWLX",queryCgxsTaskinfo.getRwlx());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getRwzt(),null)){
                    wrapper.eq("RWZT",queryCgxsTaskinfo.getRwzt());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getRwwczt(),null)){
                    wrapper.eq("RWWCZT",queryCgxsTaskinfo.getRwwczt());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getOrdernums(),null)){
                    wrapper.like("ORDERNUMS",queryCgxsTaskinfo.getOrdernums());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getGOODSNAME(),null)){
                    wrapper.like("GOODSNAME",queryCgxsTaskinfo.getGOODSNAME());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(queryCgxsTaskinfo.getNAME(),null)){
                    wrapper.eq("KHXX",queryCgxsTaskinfo.getNAME());
                }
              page = super.selectPage(page, wrapper);
        List<CgxsTaskinfo> cgxsTaskinfos = page.getRecords();
        for (CgxsTaskinfo cgxsTaskinfo : cgxsTaskinfos) {

            if (null!=cgxsTaskinfo.getFzry()&&!"".equals(cgxsTaskinfo.getFzry())){
                JyUser jyUser = jyUserMapper.selectById(cgxsTaskinfo.getFzry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    cgxsTaskinfo.setFzryName(jyUser.getName());
                }else {
                    cgxsTaskinfo.setFzryName(cgxsTaskinfo.getFzry());
                }
            }
            if (null!=cgxsTaskinfo.getCwry()&&!"".equals(cgxsTaskinfo.getCwry())){
                JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getCwry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    cgxsTaskinfo.setCwry(jyUser.getName());
                }

            }
            if (null!=cgxsTaskinfo.getKfry()&&!"".equals(cgxsTaskinfo.getKfry())){
                JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getKfry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    cgxsTaskinfo.setKfry(jyUser.getName());
                }

            }
            if(null!=cgxsTaskinfo.getXsry()&&!"".equals((cgxsTaskinfo.getXsry()))){
                JyUser jyUser=jyUserMapper.selectById(cgxsTaskinfo.getXsry());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyUser,null)){
                    cgxsTaskinfo.setXsry(jyUser.getName());
                }

            }
            if(AdscmBaseUtils.getInstance().isNotEmpty(cgxsTaskinfo.getGoodsname(),null)){
                JyGoods jyGoods = jyGoodsMapper.selectById(cgxsTaskinfo.getGoodsname());
                if (AdscmBaseUtils.getInstance().isNotEmpty(jyGoods,null)){
                    cgxsTaskinfo.setGoodsname(jyGoods.getName());
                }

            }

            if (AdscmBaseUtils.getInstance().isNotEmpty(cgxsTaskinfo.getKhxx(),null)){


                Wrapper<Supplier> jyCustomerProviderWrapper =new EntityWrapper<>();
                jyCustomerProviderWrapper.eq("code",cgxsTaskinfo.getKhxx());
                List<Supplier> suppliers = supplierMapper.selectList(jyCustomerProviderWrapper);
                if (suppliers!=null && suppliers.size()>0){
                    Supplier supplier = suppliers.get(0);
                    cgxsTaskinfo.setKhxxName(supplier.getName());
                }else {
                    Supplier supplier = supplierMapper.selectById(cgxsTaskinfo.getKhxx());
                    if (AdscmBaseUtils.getInstance().isNotEmpty(supplier,null)){
                        cgxsTaskinfo.setKhxxName(supplier.getName());
                    }
                }
            }

        }
        page.setTotal(super.selectCount(wrapper));

        return page;
    }


}
